﻿@using ServiceStack;
@{
    Layout = "";
}
<style>
    .ngdialog.ngdialog-theme-plain .ngdialog-content {
        width: 40% !important;
    }
</style>
<h3>员工管理</h3>

<div class="panel panel-default" ng-controller="UserManageController" ng-class="{ 'whirl standard': isLoading}">
    <div class="panel-heading">
        <div class="form-inline ng-pristine ng-valid">
            <div class="form-group" style="width:100%;">
                关键字:<input id="input-email" placeholder="用户名或姓名或手机号" class="form-control" type="text" ng-model="Keywords">
                分组:<select style="width: 10%;max-width: 20%;" ng-options="m.Id as m.GroupName for m in UserGroupList" ng-model="UserGroupId" class="form-control">
                    <option value="">全部</option>
                </select>
                状态:<select style="width: 10%" ng-init="UserState=-1" ng-model="UserState" class="form-control">
                    <option value="-1">全部</option>
                    <option value="0">正常</option>
                    <option value="1">冻结</option>
                </select>
                <button type="submit" class="btn btn-labeled btn-primary" ng-click="tableParams.$params.page = 1;tableParams.reload()">
                    <span class="btn-label">
                        <em class="fa fa-search"></em>
                    </span>搜索
                </button>
                <button type="submit" class="btn btn-labeled btn-success" ng-click="enployeeManagementUpdataView(0)">
                    <span class="btn-label">
                        <em class="fa fa-plus"></em>
                    </span>新增
                </button>
            </div>
        </div>
    </div>
    <div class="panel-body">
        <div class="table-responsive">
            <table ng-table="tableParams" class="table table-striped table-bproducted table-hover">
                <tbody>
                    <tr ng-repeat="employee in $data">
                        <td data-title="'用户名'" class="text-center">{{employee.UserName}}</td>
                        <td data-title="'姓名'" class="text-center">{{employee.RealName}}</td>
                        <td data-title="'邮箱'" class="text-center">{{employee.Email}}</td>
                        <td data-title="'电话'" class="text-center">{{employee.Phone}}</td>
                        <td data-title="'分组'" class="text-center">{{employee.GroupName}}</td>
                        <td data-title="'状态'" class="text-center">{{convertToEnum(employee.UserState,enumUserState)}}</td>
                        <td data-title="'添加时间'" class="text-center">{{employee.AddTime}}</td>
                        <td data-title="'操作'" nowrap="nowrap" class="text-center">
                            <span ng-show="!employee.IsAdmin">
                                <button type="button" ng-if="employee.UserState==1" ng-click="changeUserState(employee,0)" class="btn btn-sm btn-success" title="激活">
                                    <em class="fa fa-unlock"></em>
                                </button>
                                <button type="button" ng-if="employee.UserState==0" ng-click="changeUserState(employee,1)" class="btn btn-sm btn-warning" title="冻结">
                                    <em class="fa fa-lock"></em>
                                </button>
                                <button type="button" ng-click="enployeeManagementUpdataView(employee.Id)" class="btn btn-sm btn-edit" title="编辑">
                                    <em class="fa fa-edit"></em>
                                </button>
                                <button type="button" ng-click="changePassWord(employee.Id)" class="btn btn-sm btn-primary" title="修改密码">
                                    <em class="fa fa-unlock-alt"></em>
                                </button>
                                <button type="button" ng-if="employee.Id!=user.Id" ng-click="removeEmployeeCenter($index,employee)" class="btn btn-sm btn-danger" title="删除">
                                    <em class="fa fa-trash-o"></em>
                                </button>
                            </span>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
<script type="text/ng-template" id="changePasswordDialogId">
    <div class="ngdialog-message">
        <div class="panel panel-default">
            <div class="panel-body">
                <fieldset class="b0">
                    <legend>修改密码</legend>
                </fieldset>
                <fieldset>
                    <div class="form-ingroup">
                        <label class="col-sm-2 control-label">新密码</label>
                        <div class="col-sm-4">
                            <input type="password" name="newPassword" required="" ng-model="PasswordContent.newPassword" class="form-control" maxlength="20" />
                        </div>

                        <div class="col-sm-2">
                            <h4 class="text-danger" style="color:red">*</h4>
                        </div>
                        <span ng-show="IsEnoughLen" class="text-danger">密码长度最少6位，最多20位</span>
                    </div>
                </fieldset>
                <fieldset>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">确认密码</label>
                        <div class="col-sm-4">
                            <input type="password" name="sureNewPassword" required="" ng-model="PasswordContent.sureNewPassword" class="form-control" maxlength="20" />
                        </div>
                        <span ng-show="IsSamePassword" class="text-danger">两次密码不一致</span>
                        <div class="col-sm-2">
                            <h4 class="text-danger" style="color:red">*</h4>
                        </div>
                    </div>
                </fieldset>
            </div>
            <div class="panel-footer text-right">
                <button type="button" class="btn btn-default" ng-click="closeThisDialog()">
                    关闭
                </button>
                <button type="button" class="btn btn-default" ng-click="serveThisMessage()">
                    确定
                </button>
            </div>
        </div>
    </div>
</script>
<script type="text/ng-template" id="deleteConfirmDialog">
    <div class="ngdialog-message text-center">
        <h3>确定要删除吗?</h3>
        <p>删除后不可恢复!</p>
        <button type="button" ng-click="confirm()" class="btn btn-primary mr">确认</button>
        <button type="button" ng-click="closeThisDialog('button')" class="btn btn-default">取消</button>
    </div>
</script>
<script type="text/ng-template" id="changestateConfirmDialog">
    <div class="ngdialog-message text-center">
        <h3>确定要{{changeUserStateText}}该用户吗?</h3>
        <button type="button" ng-click="confirm()" class="btn btn-primary mr">确认</button>
        <button type="button" ng-click="closeThisDialog('button')" class="btn btn-default">取消</button>
    </div>
</script>
<script>
    App.controller('UserManageController', [
        '$scope', '$resource', '$state', '$route', '$http', 'ngTableParams', 'ngTableDataService', 'Notify', '$filter', 'ngDialog',
        function ($scope, $resource, $state, $route, $http, ngTableParams, ngTableDataService, Notify, $filter, ngDialog) {
            'use strict';
            $scope.isLoading = true;
            $scope.IsSamePassword = false;
            $scope.IsEnoughLen = false;
            $scope.PasswordContent = [];
            //console.log($scope.user);
            //员工数据查询
            $scope.tableParams = new ngTableParams({
                page: 1,
                count: 10,
                sorting: {
                    Id: 'desc' // 初始化排序
                }
            }, {
                getData: function ($defer, params) {
                    $scope.isLoading = true;
                    var requestParams = {
                        Skip: (params.page() - 1) * params.count(),
                        Take: params.count(),
                        Keywords: $scope.Keywords,
                        UserGroupId:$scope.UserGroupId,
                        UserState:$scope.UserState
                    };
                    $http.post('/User/UserManage', ngTableDataService.getServerSideParams(params.orderBy().toString(), params, requestParams))
                        .success(function (data, status) {

                            params.total(data.Total);
                            $defer.resolve(data.Results);
                            $scope.isLoading = false;
                        });
                }
            });

            //枚举值获取
            $scope.enumUserState = @(Html.Raw(WsdShop.Common.EnumUtil.SelectListEnum<WsdShop.Model.Enum.UserStateEnum>(null, false).ToJson()));

            //分组获取
            $http.post("/User/GetUserGroupList").then(function (response) {
                $scope.UserGroupList = response.data;
            });



            //跳转到更新新增页面
            $scope.enployeeManagementUpdataView = function (id) {
                $state.go('app.usermanageedit', { "Id": id });
            }

            //修改密码点击
            $scope.changePassWord = function (employeeId) {
                $scope.TempChangePasswordId = employeeId;
                ngDialog.openConfirm({
                    template: 'changePasswordDialogId',
                    className: 'ngdialog-theme-plain',
                    scope: $scope
                }).then(function (value) {
                }, function (reason) {
                }
                );
            }
            //获取密码长度
            $scope.getPwdLength = function (password) {
                if (password == null) { $scope.passwodrLen = 0; }
                else {
                    $scope.passwodrLen = password.replace(/[^x00-xff]/g, "01").length;
                }
            }
            //确认点击
            $scope.serveThisMessage = function () {
                if (!$scope.PasswordContent.newPassword || $scope.PasswordContent.newPassword.length == 0) {
                    Notify.alert("请输入新密码", { status: 'warning' });
                    return false;
                }
                if (!$scope.PasswordContent.sureNewPassword || $scope.PasswordContent.sureNewPassword.length == 0) {
                    Notify.alert("请输入确认密码", { status: 'warning' });
                    return false;
                }
                $scope.getPwdLength($scope.PasswordContent.newPassword);
                if ($scope.passwodrLen < 6) {
                    $scope.IsEnoughLen = true;
                    if ($scope.PasswordContent.sureNewPassword == $scope.PasswordContent.newPassword) {
                        $scope.IsSamePassword = false;
                    }
                    return false;
                } else {
                    $scope.IsEnoughLen = false;
                }
                if ($scope.PasswordContent.sureNewPassword != $scope.PasswordContent.newPassword) {
                    $scope.IsSamePassword = true;
                    return false;
                } else {
                    $scope.IsSamePassword = false;
                }
                $scope.isLoading = true;
                $http.post("/User/UpdateUserPassWord", { NewPassword: $scope.PasswordContent.newPassword, Id: $scope.TempChangePasswordId }).then(function (returnData) {
                    if (returnData.data.isSuccess) {
                        Notify.alert('操作成功!', { status: 'success' });
                        ngDialog.closeAll();
                        $scope.tableParams.$params.page = 1;
                        $scope.tableParams.reload();
                    } else {
                        Notify.alert(returnData.data.msg, { status: 'warning' });
                    }

                });
            }


            //删除员工
            $scope.removeEmployeeCenter = function (index, enployee) {
                ngDialog.openConfirm({
                    template: 'deleteConfirmDialog',
                    className: 'ngdialog-theme-default'
                }).then(function (value) {
                    $scope.isLoading = true;
                    $http.post("/User/DelUser", { Id: enployee.Id }).then(function (returnData) {
                        if (returnData.data.isSuccess) {
                            $scope.tableParams.data.splice(index, 1);
                            Notify.alert('操作成功!', { status: 'success' });
                        } else {
                            Notify.alert(returnData.data.msg, { status: 'warning' });
                        }
                        $scope.isLoading = false;
                    });
                }, function (reason) {
                }
                );
            }

            //激活、冻结员工
            $scope.changeUserState=function(employee,userstate){
                $scope.changeUserStateText=(userstate==0?"激活":"冻结");
                ngDialog.openConfirm({
                    template: 'changestateConfirmDialog',
                    className: 'ngdialog-theme-default',
                    scope:$scope
                }).then(function (value) {
                    $scope.isLoading = true;
                    $http.post("/User/ChangeUserState", { Id: employee.Id,UserState:userstate }).then(function (returnData) {
                        if (returnData.data.isSuccess) {
                            employee.UserState=userstate;
                            Notify.alert('操作成功!', { status: 'success' });
                        } else {
                            Notify.alert(returnData.data.msg, { status: 'warning' });
                        }
                        $scope.isLoading = false;
                    });
                }, function (reason) {
                }
                );
            }


        }
    ]);
</script>
